package org.zerg.service.impl;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zerg.dao.BannerDao;
import org.zerg.dao.ImageDao;
import org.zerg.dto.FileDto;
import org.zerg.entity.Banner;
import org.zerg.entity.BannerItem;
import org.zerg.entity.Image;
import org.zerg.service.BannerService;

@Service
public class BannerServiceImpl extends BaseServiceImpl implements BannerService{
	
	@Autowired
	private BannerDao bannerDao;
	
	@Autowired
	private ImageDao imageDao;
	
	@Value("${adImage.url}")
	private String adImageUrl;
	
	@Value("${adImage.savePath}")
	private String savePath;

	public Banner getBannerById(int id) {
		Banner banner = bannerDao.getBannerById(id);
		List<BannerItem> list = bannerDao.getBannerItemById(id);
		for (BannerItem bannerItem : list) {
			bannerItem.getImage().setUrl(adImageUrl+bannerItem.getImage().getUrl());
		}
		banner.setBannerItem(list);
		return banner;
	}

	public List<BannerItem> getBannerList() {
		List<BannerItem> list = bannerDao.getBannerList(1);
		for (BannerItem bannerItem : list) {
			bannerItem.getImage().setUrl(adImageUrl+bannerItem.getImage().getUrl());
		}
		return list;
	}

	public void modifyBanner(int id,FileDto dto,int classId) throws IllegalStateException, IOException {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image img = imageDao.getImage("/"+fileName);
		bannerDao.modifyBanner(id, img.getId(), classId);
	}

	public void modifyBannerNoFile(int id, int pid) {
		bannerDao.modifyBannerNoFile(id, pid);
	}

	

}
